//
//  DayCell.m
//  TestCalendar
//
//  Created by yull on 14/12/2.
//  Copyright (c) 2014年 yull. All rights reserved.
//

#import "DayCell.h"
#import "UIView+Frame.h"

@implementation DayCell

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        CGRect rect = CGRectMake(0, 5, self.contentView.width, 20);
        self.titleLabel = [[UILabel alloc] initWithFrame:rect];
        self.titleLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        self.titleLabel.textColor = RGB(136, 118, 102);
        self.titleLabel.backgroundColor = [UIColor clearColor];
        [self.contentView addSubview:self.titleLabel];
        
        rect = CGRectMake(10, self.titleLabel.bottom + 5, self.titleLabel.width-20, self.titleLabel.width-20);
        self.iconImageView1 = [[UIImageView alloc] initWithFrame:rect];
        [self.contentView addSubview:self.iconImageView1];
        
        self.iconImageView2 = [[UIImageView alloc] initWithFrame:rect];
        [self.contentView addSubview:self.iconImageView2];
        
        self.iconImageView3 = [[UIImageView alloc] initWithFrame:rect];
        [self.contentView addSubview:self.iconImageView3];
        
        rect = CGRectMake(self.contentView.width - 24, self.contentView.height - 24, 24, 24);
        self.finishImageView = [[UIImageView alloc] initWithFrame:rect];
        self.finishImageView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleTopMargin;
        self.finishImageView.image = [UIImage imageNamed:@"sport_finished_icon"];
        [self.contentView addSubview:self.finishImageView];
        self.finishImageView.hidden = YES;
    }
    return self;
}

- (void)refreshCellImage
{
    self.iconImageView1.hidden = !(self.planArray.count > 0);
    self.iconImageView2.hidden = !(self.planArray.count > 1);
    self.iconImageView3.hidden = !(self.planArray.count > 2);
    
    NSInteger finishedCount = 0;
    
    if (self.planArray.count > 0) {
        PlanInfo *info = self.planArray.firstObject;
        self.iconImageView1.image = [UIImage imageNamed:[info getIconImageName]];
        
        if (info.status == PlanInfoStatusEnd) {
            finishedCount ++;
        }
    }
    
    if (self.planArray.count > 1) {
        PlanInfo *info = [self.planArray objectAtIndex:1];
        self.iconImageView2.image = [UIImage imageNamed:[info getIconImageName]];
        
        if (info.status == PlanInfoStatusEnd) {
            finishedCount ++;
        }
    }
    
    if (self.planArray.count > 2) {
        PlanInfo *info = self.planArray.lastObject;
        self.iconImageView3.image = [UIImage imageNamed:[info getIconImageName]];
        
        if (info.status == PlanInfoStatusEnd) {
            finishedCount ++;
        }
    }
    
    if (finishedCount > 0) {
        self.finishImageView.hidden = NO;
    }else {
        self.finishImageView.hidden = YES;
    }
    
    if (self.planArray.count == 1) {
        CGFloat width = self.titleLabel.width-8;
        CGRect rect = CGRectMake(5, self.titleLabel.bottom, width, width);
        self.iconImageView1.frame = rect;
    }else if (self.planArray.count == 2) {
        CGFloat width = self.titleLabel.width-14;
        CGRect rect = CGRectMake(8, self.titleLabel.bottom+3, width, width);
        self.iconImageView1.frame = rect;
        self.iconImageView2.frame = rect;
        
        self.iconImageView1.centerX -= 5;
        self.iconImageView2.centerX += 5;
    }else if (self.planArray.count == 3) {
        CGFloat width = self.titleLabel.width-20;
        CGRect rect = CGRectMake(11, self.titleLabel.bottom +5, width, width);
        
        self.iconImageView1.frame = rect;
        self.iconImageView2.frame = rect;
        self.iconImageView3.frame = rect;
        self.iconImageView1.centerX -= 8;
        self.iconImageView3.centerX += 8;
    }
}
@end
